package org.fh.inductionLamp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.fh.domain.DataFrame;
import org.fh.domain.LampStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

/**
 * 状态处理器
 * @Author: wwy
 * @Date: 2020/10/23 14:35
 */
public class StatusHandle {
    private static final Logger LOGGER = LoggerFactory.getLogger(StatusHandle.class);
    public static LampStatus lampStatus;
    public static ApplicationContext applicationContext;
    /**
     * 状态处理器
     * @param dataFrame
     */
    public void handle(DataFrame dataFrame){
        String dataStr = dataFrame.getDataStr();
        String yellowLightFrequency = Integer.parseInt(dataStr.substring(0, 2), 16)+"";
        String yellowLightLuminance = Integer.parseInt(dataStr.substring(2, 4), 16)+"";
        String redLightFrequency = Integer.parseInt(dataStr.substring(4, 6), 16)+"";
        String redLightLuminance = Integer.parseInt(dataStr.substring(6, 8), 16)+"";
        String carInspectionDeviceStatus = Integer.parseInt(dataStr.substring(8,10),16)+"";
        String closeTime = Integer.parseInt(dataStr.substring(12,14)+dataStr.substring(10, 12), 16)+"";
        String startTime = Integer.parseInt(dataStr.substring(16,18)+dataStr.substring(14, 16), 16)+"";
        String currentVisibility = Integer.parseInt(dataStr.substring(20,22)+dataStr.substring(18, 20), 16)+"";
        String forecastVisibility = Integer.parseInt(dataStr.substring(24,26)+dataStr.substring(22, 24), 16)+"";
        if(lampStatus == null){
            lampStatus = new LampStatus();
        }
        lampStatus.setYellowLightFrequency(yellowLightFrequency);
        lampStatus.setYellowLightLuminance(yellowLightLuminance);
        lampStatus.setRedLightFrequency(redLightFrequency);
        lampStatus.setRedLightLuminance(redLightLuminance);
        lampStatus.setCarInspectionDeviceStatus(carInspectionDeviceStatus);
        lampStatus.setCloseTime(closeTime);
        lampStatus.setStartTime(startTime);
        lampStatus.setCurrentVisibility(currentVisibility);
        lampStatus.setForecastVisibility(forecastVisibility);
        lampStatus.setDeviceId(Integer.parseInt(dataFrame.getSrcID().highLow,16)+"");
        //通过http进行回调
        this.callBackData();
    }

    public void callBackData(){
        RestTemplate restTemplate = applicationContext.getBean(RestTemplate.class);
        Environment environment = applicationContext.getEnvironment();
        String url = environment.getProperty("lamp.callBackUrl");
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        String s1 = JSON.toJSONString(lampStatus);
        LOGGER.info("回调参数：{},回调地址:{}",s1,url);
        HttpEntity<String> formEntity = new HttpEntity<String>(s1, headers);
        String s= restTemplate.postForEntity(url,formEntity,String.class).getBody();

    }
}
